package com.greateamd.timetableasy.client.widgets.login;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.greateamd.timetableasy.client.Framework;
import com.greateamd.timetableasy.client.services.login.LoginService;
import com.greateamd.timetableasy.client.services.login.LoginServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;

public class LoginPanel extends Composite {

	@UiTemplate("LoginPanel.ui.xml")
	interface LoginPageUIBinder extends UiBinder<Widget, LoginPanel> {}
	private static LoginPageUIBinder uiBinder = GWT.create(LoginPageUIBinder.class);
	
	@UiField FormPanel form;
	@UiField TextBox username;
	@UiField PasswordTextBox password;
	@UiField CheckBox rememberme;
	@UiField Button submitBt;
	@UiField Label state;
	
	public LoginPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		buildForm();
	}

	private void buildForm() {
		form.addSubmitCompleteHandler(handlerSubmitComplet);
	    form.setMethod(FormPanel.METHOD_POST);
	}
	
	@UiHandler("submitBt")
	void handleClick(ClickEvent e) {
		if (username.getText().length() == 0 || password.getText().length() == 0) {
			   Window.alert("Username or password is empty.");  
		} else {
			form.submit();
		}
	}

	private final SubmitCompleteHandler handlerSubmitComplet = new SubmitCompleteHandler() {
		@Override
		public void onSubmitComplete(SubmitCompleteEvent event) {
//			redirectByReplace("/timetableasy/timetableasy.html"); //mode prod
			redirectByReplace("http://127.0.0.1:8888/timetableasy.html?gwt.codesvr=127.0.0.1:9997");
		}
	};

	private native void redirectByReplace(String url) /*-{
	    $wnd.location.replace(url);
	}-*/;
	
//	private final ClickHandler clickSubmitButtonHandler = new ClickHandler() {
//		@Override
//		public void onClick(ClickEvent event) {
//			if (username.getText().length() == 0 || password.getText().length() == 0) {
//				   Window.alert("Username or password is empty.");  
//			} else {
//				form.submit();
//				tryLogin();
//			}
//		}
//	};
	
//	private void tryLogin(){
//		final LoginServiceAsync loginService = GWT.create(LoginService.class);
//		new ServerCall<String>(){
//
//			@Override
//			protected void callService(AsyncCallback<String> asyncCallback) {
//				loginService.login(username.getValue(), password.getValue(), asyncCallback);
//			}
//
//			@Override
//			public void onSuccess(String result) {
//				if (result == null){
//					state.setText("Failed to log in");
//				} else {
//					Framework.get().setUser(result);
//					state.setText("Connection success...");
//				}
//			}
//		}.call();
//	}
//	
	public static final Command cmdOfferLoginPopup = new Command() {
		@Override
		public void execute() {
			showLoginPopup();
		}

		void showLoginPopup(){
			final DialogBox db = new DialogBox(false, true);
			db.setGlassEnabled(true);
			db.setText("Please log in");
			final VerticalPanel vp = new VerticalPanel();
			db.setWidget(vp);
			
			final TextBox tb = new TextBox();
			tb.setTitle("Username");
			final PasswordTextBox pb = new PasswordTextBox();
			pb.setTitle("Password");
			
			vp.add(tb);
			vp.add(pb);

			vp.add(new Button("Log in", new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					final LoginServiceAsync loginService = GWT.create(LoginService.class);
					new ServerCall<String>(){

						@Override
						protected void callService(AsyncCallback<String> asyncCallback) {
							loginService.login(tb.getValue(), pb.getValue(), asyncCallback);
						}

						@Override
						public void onSuccess(String result) {
							if (result == null){
								vp.add(new Label("Failed to log in"));
							} else {
								Framework.get().setUser(result);
								db.hide();
							}
						}
					}.call();
				}
			})
			);

			db.center();
		}
	};
}
